package cn.tom.mvc;


import cn.tom.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginTnterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
        System.out.println("拦截器执行了@@@@@");
        Object user =request.getSession().getAttribute("who");
        User user1=(User)user;
        String path=request.getServletPath();
        System.out.println(path);
//        获取信息
        if (user!=null){
            System.out.println("用户已经登录正常");
            if (user1.getRole()!="adm"&&path.startsWith("/adm")){
                System.out.println("停止访问");

                response.sendRedirect("/user/go2login");
                return false;
            }else if (user1.getRole()!="stu"&&path.startsWith("/stu")){
                System.out.println("停止访问");
//      转发登录界面
                response.sendRedirect("/user/go2login");
                return false;
            }else if (user1.getRole()!="tea"&&path.startsWith("/tea")){
                System.out.println("停止访问");
//      转发登录界面
                response.sendRedirect("/user/go2login");
                return false;
            }
            return true;
        }else {
            System.out.println("用户没有登录，就转发到登录界面");
//      转发登录界面
            response.sendRedirect("/user/go2login");
            return false;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request,HttpServletResponse response,Object handler,ModelAndView modelAndView) throws Exception{
        HandlerInterceptor.super.postHandle(request,response,handler,modelAndView);
    }
    @Override
    public void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object hander,Exception ex) throws Exception{
        HandlerInterceptor.super.afterCompletion(request,response,hander,ex);
    }
}
